package com.example.myapplication; // 定义包名

import android.os.Bundle; // 导入 Bundle 类，用于存储和传递活动的状态信息
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity; // 导入 AppCompatActivity 类，它是支持向后兼容的 Activity 类
import androidx.fragment.app.Fragment; // 导入 Fragment 类，定义片段行为
import androidx.fragment.app.FragmentManager; // 导入 FragmentManager 类，管理片段的生命周期
import androidx.fragment.app.FragmentTransaction; // 导入 FragmentTransaction 类，执行片段事务（添加、移除、替换）

// 定义 Fragment_MainActivity 类，继承自 AppCompatActivity
public class Fragment_MainActivity extends AppCompatActivity implements View.OnClickListener {

    Fragment fragment1, fragment2, fragment3, fragment4;
    FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_main); // 确保这是你的主布局

        // 初始化 Fragment
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();

        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        // 添加 Fragment 并初始隐藏
        transaction.add(R.id.fragment_container, fragment1);
        transaction.add(R.id.fragment_container, fragment2);
        transaction.add(R.id.fragment_container, fragment3);
        transaction.add(R.id.fragment_container, fragment4);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
        transaction.commit();

        // 设置按钮并监听，确保这些按钮在 activity_fragment_main.xml 中存在
        Button button1 = findViewById(R.id.filestreambutton1);
        Button button2 = findViewById(R.id.button2);
        Button button3 = findViewById(R.id.button3);
        Button button4 = findViewById(R.id.button4);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Fragment fragmentToShow = null;

        if (view.getId() == R.id.filestreambutton1) {
            fragmentToShow = fragment1;
        } else if (view.getId() == R.id.button2) {
            fragmentToShow = fragment2;
        } else if (view.getId() == R.id.button3) {
            fragmentToShow = fragment3;
        } else if (view.getId() == R.id.button4) {
            fragmentToShow = fragment4;
        }

        if (fragmentToShow != null) {
            showFragment(fragmentToShow);
        }
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction transaction = manager.beginTransaction();
        // 隐藏所有 Fragment
        transaction.hide(fragment1);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
        // 显示所选的 Fragment
        transaction.show(fragment);
        transaction.commit();
    }
}

